import * as Types from "@/store/action-types.js";
import { toLogin, validate } from "@/api/user";
const profileActions = {
  async [Types.SET_USER]({ commit }, { userInfo, has }) {
    commit(Types.SET_USER, userInfo);
    commit(Types.SET_PERMISSIOM, has);
  },
  async [Types.SET_LOGIN]({ commit, dispatch }, payload) {
    console.log(payload);
    let userInfo = await toLogin(payload);
    dispatch(Types.SET_USER, { userInfo, has: true });
    // commit(Types.SET_LOGIN, payload);
  },
  async [Types.VALIDATE]({ commit, dispatch }, payload) {
    // commit(Types.VALIDATE, payload);
    // 此时需要 看一下用户是否登录过
    if (!localStorage.getItem("token")) return false;
    // 这是需要在axios中 请求时 增加token， 传递到后端， 让他去验证去
    try {
      let userInfo = await validate();
      dispatch(Types.SET_USER, { userInfo, has: true });
      return true;
    } catch (e) {
      dispatch(Types.SET_USER, { userInfo: {}, has: false });
      return false;
    }
  },
};
export default profileActions;
